package one.bugu.android.demon.ui.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.MedalBean;
import one.bugu.android.demon.constant.MedalContant;
import one.bugu.android.demon.util.OnViewClickListener;


/**
 * 带图片的进度条
 */
public class MedalProgressView extends LinearLayout {

    private Context mContext;
    private View layoutView;
    private ProgressBar pb_item_progressBar;
    private LinearLayout ll_rank_image;

    public MedalProgressView(Context context) {
        this(context, null);
    }

    public MedalProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public MedalProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;
        this.setOrientation(HORIZONTAL);

        //初始化进度条
        initView();
    }

    /**
     * 初始化view, 添加布局
     */
    private void initView() {
        if (mContext == null) {
            return;
        }
        layoutView = View.inflate(mContext, R.layout.layout_custom_progress_bar, null);
        pb_item_progressBar = layoutView.findViewById(R.id.pb_item_progressBar);
        pb_item_progressBar.setVisibility(GONE);
        ll_rank_image = layoutView.findViewById(R.id.ll_rank_image);
        this.addView(layoutView);
    }

    /**
     * 设置数据
     *
     * @param imageData
     * @param imageHeight 图片默认高度
     */
    public void setLayoutViewData(List<MedalBean> imageData, double value, int imageHeight) {
        pb_item_progressBar.setVisibility(VISIBLE);
        setDefViewData(imageData, value, imageHeight);
    }

    /**
     * 设置默认显示数据
     */
    private void setDefViewData(List<MedalBean> imageData, double value, int imageHeight) {
        if (mContext == null) {
            return;
        }
        if (imageData == null) {
            return;
        }
        //bgt数量
        int position = 0;
        //单个条目宽度
        int itemWidth = MedalContant.MEDAL_HORIZONTAL_SPACE;
        ll_rank_image.removeAllViews();
        //设置父布局宽度
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ll_rank_image.getLayoutParams();
        layoutParams.width = dpToPx(imageData.size() * itemWidth);
        ll_rank_image.setLayoutParams(layoutParams);
        //设置progress宽度
        RelativeLayout.LayoutParams pblayoutParams = (RelativeLayout.LayoutParams) pb_item_progressBar.getLayoutParams();
        pblayoutParams.width = dpToPx(imageData.size() * itemWidth);
        pblayoutParams.topMargin = dpToPx(imageHeight / 2);
        pb_item_progressBar.setLayoutParams(pblayoutParams);

        //计算两个等级之间的进度
        int p = 100 / imageData.size();
        //将各个点位置图片添加在父布局中
        for (int i = 0; i < imageData.size(); i++) {
            final MedalBean progressData = imageData.get(i);
            if (progressData == null) {
                continue;
            }
            //计算进度
            if (value >= imageData.get(i).getMinNum()) {
                position = i;
            }
            //添加头衔
            View itemView = View.inflate(mContext, R.layout.item_progress_view, null);
            //动态设置权重
            LinearLayout.LayoutParams lp = new LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
            itemView.setLayoutParams(lp);
            //图标
            final ImageView iv_item_image = itemView.findViewById(R.id.iv_item_image);
            //由于图标大小不一致, 此处动态设置图标大小
            LinearLayout.LayoutParams lvp = (LayoutParams) iv_item_image.getLayoutParams();
            lvp.width = dpToPx(imageData.get(i).getImageWidth());
            lvp.height = dpToPx(imageData.get(i).getImageHeight());
            iv_item_image.setLayoutParams(lvp);
            //名称
            TextView tv_item_text = itemView.findViewById(R.id.tv_item_text);
            iv_item_image.setImageResource(progressData.getDrawableRes());
            //等级图标
            iv_item_image.setSelected(!(value > progressData.getMinNum()));
            tv_item_text.setText(progressData.getTitleName());
            ll_rank_image.addView(itemView);
            //按钮是否可点击
            itemView.setClickable(progressData.isCanClick());
            //点击事件
            final int finalI = i;
            itemView.setOnClickListener(new OnViewClickListener() {
                @Override
                public void singleClick(View view) {
                    if (onViewClickEvent != null) {
                        onViewClickEvent.onViewLick(finalI);
                    }
                }
            });
            itemView.setClickable(progressData.isCanClick());
        }
        //设置进度
        //当前头衔区域进度
        if (position < imageData.size() - 1) {
            double maxNum = imageData.get(position + 1).getMaxNum();
            double minNum = imageData.get(position + 1).getMinNum();
            double bgtD = value - minNum;
            int bp = (int) ((bgtD / (maxNum - minNum)) * p);
            //完成区域进度
            int curProgress = (position + 1) * p;
            pb_item_progressBar.setProgress((curProgress + bp) - 1);
        } else {
            pb_item_progressBar.setProgress(100);
        }

    }

    /**
     * 领取
     */
    public void getGive(int position) {
        if (ll_rank_image == null) {
            return;
        }
        View childView = ll_rank_image.getChildAt(position);
        ImageView iv_item_image = childView.findViewById(R.id.iv_item_image);
        boolean selected = iv_item_image.isSelected();
        iv_item_image.setSelected(!selected);
        childView.setClickable(false);
        invalidate();
    }

    private OnViewClickEvent onViewClickEvent;

    public interface OnViewClickEvent {
        void onViewLick(int position);
    }

    public void setOnViewClickEvent(OnViewClickEvent onViewClickEvent) {
        this.onViewClickEvent = onViewClickEvent;
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     */
    public int dpToPx(float dp) {
        return (int) (dp * getResources().getDisplayMetrics().density);
    }
}
